package org.bsplus.client.mvp;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.event.Observable;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.FormButtonBinding;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.SimpleComboBox;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.user.client.ui.Widget;
import java.util.List;
import org.bsplus.rpc.dto.IssueSeverityDTO;
import org.bsplus.rpc.dto.IssueTypeDTO;
import org.bsplus.rpc.dto.ProjectDTO;
import org.bsplus.rpc.dto.UserDTO;

public class CreateIssueView extends FormPanel implements CreateIssuePresenter.Display
{
    private SimpleComboBox<IssueTypeDTO> issueType = new SimpleComboBox<IssueTypeDTO>();
    private SimpleComboBox<IssueSeverityDTO> issueSeverity = new SimpleComboBox<IssueSeverityDTO>();
    private SimpleComboBox<ProjectDTO> project = new SimpleComboBox<ProjectDTO>();
    private SimpleComboBox<UserDTO> assignee = new SimpleComboBox<UserDTO>();
    private TextField<String> summary = new TextField<String>();
    private TextArea description = new TextArea();
    private Button createButton = new Button("Create");

    public CreateIssueView()
    {
        setFrame(true);
        setScrollMode(Scroll.AUTO);
        setHeading("Create Issue");
      
        FieldSet fieldSet = new FieldSet();
        fieldSet.setWidth(600);
        fieldSet.setHeading("Create a new issue");

        FormLayout layout = new FormLayout();
        layout.setLabelWidth(100);
        fieldSet.setLayout(layout);
        
        issueType.setFieldLabel("Issue Type");
        issueType.setForceSelection(true);
        issueType.setEditable(false);
        issueType.setAllowBlank(false);
        fieldSet.add(issueType);

        issueSeverity.setFieldLabel("Issue Severity");
        issueSeverity.setForceSelection(true);
        issueSeverity.setEditable(false);
        issueSeverity.setAllowBlank(false);
        fieldSet.add(issueSeverity);

        project.setFieldLabel("Project");
        project.setForceSelection(true);
        project.setEditable(false);
        project.setAllowBlank(false);
        fieldSet.add(project);

        assignee.setFieldLabel("Assignee");
        assignee.setForceSelection(true);
        assignee.setEditable(false);
        assignee.setAllowBlank(false);
        fieldSet.add(assignee);

        summary.setFieldLabel("Summary");
        summary.setAllowBlank(false);
        fieldSet.add(summary);

        
        description.setFieldLabel("Description");
        description.setAllowBlank(false);
        fieldSet.add(description);

        add(fieldSet);

        setButtonAlign(HorizontalAlignment.LEFT);

        addButton(createButton);
       
        FormButtonBinding formButtonBinding = new FormButtonBinding(this);
        formButtonBinding.addButton(createButton);
        formButtonBinding.startMonitoring();
    }

    @Override
    public Widget asWidget()
    {
        return this;
    }

    @Override
    public void startProcessing()
    {
    }

    @Override
    public void stopProcessing()
    {
    }

    @Override
    public void init()
    {
        issueType.setValue(null);
        issueSeverity.setValue(null);
        project.setValue(null);
        assignee.setValue(null);
        summary.setValue("");
        description.setValue("");
    }

    @Override
    public void setIssueTypes(List<IssueTypeDTO> issueTypes)
    {
        issueType.removeAll();
        for(IssueTypeDTO issueTypeDTO : issueTypes)
        {
            issueType.add(issueTypeDTO);
        }
    }

    @Override
    public void setIssueSeveritys(List<IssueSeverityDTO> issueSeveritys)
    {
        issueSeverity.removeAll();
        for(IssueSeverityDTO issueSeverityDTO : issueSeveritys)
        {
            issueSeverity.add(issueSeverityDTO);
        }
    }

    @Override
    public void setProjects(List<ProjectDTO> projects)
    {
        project.removeAll();
        for(ProjectDTO projectDTO : projects)
        {
            project.add(projectDTO);
        }
    }

    @Override
    public void setUsers(List<UserDTO> users)
    {
        assignee.removeAll();
        for(UserDTO user : users)
        {
            assignee.add(user);
        }
    }

    @Override
    public IssueTypeDTO getIssueType()
    {
        return issueType.getValue().getValue();
    }

    @Override
    public IssueSeverityDTO getIssueSeverity()
    {
        return issueSeverity.getValue().getValue();
    }

    @Override
    public ProjectDTO getProject()
    {
        return project.getValue().getValue();
    }

    @Override
    public String getSummary()
    {
        return summary.getValue();
    }

    @Override
    public String getDescription()
    {
        return description.getValue();
    }

    @Override
    public Observable getCreateButton()
    {
        return createButton;
    }

    @Override
    public UserDTO getAssignee()
    {
        return assignee.getValue().getValue();
    }
}
